package work.jame.blog.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import work.jame.blog.framework.exception.BusinessException;
import work.jame.blog.framework.exception.db.DataNotExistException;
import work.jame.blog.framework.exception.db.InsertDataBaseException;
import work.jame.blog.framework.exception.db.UpdateDataBaseException;
import work.jame.blog.framework.utils.NumberUtils;
import work.jame.blog.framework.utils.security.SpringSecurityUtils;
import work.jame.blog.pojo.User;
import work.jame.blog.pojo.UserArticleRiceGrains;
import work.jame.blog.mapper.UserArticleRiceGrainsMapper;
import work.jame.blog.service.ArticleService;
import work.jame.blog.service.UserArticleRiceGrainsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import work.jame.blog.service.UserRiceGrainsLogService;
import work.jame.blog.service.UserService;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jame
 * @since 2023-05-18
 */
@Service
public class UserArticleRiceGrainsServiceImpl extends ServiceImpl<UserArticleRiceGrainsMapper, UserArticleRiceGrains> implements UserArticleRiceGrainsService {


    @Resource
    private UserArticleRiceGrainsMapper userArticleRiceGrainsMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserRiceGrainsLogService userRiceGrainsLogService;


    public static void main(String[] args) {
    }

    @Override
    @Transactional
    public void feedArticlePublisher(Long articleId, Double riceGrainsNumber) {
        //检查是否通过调用接口传入非整数的米粒数
        if (!NumberUtils.checkDecimalsPresent(riceGrainsNumber)) {
            throw new BusinessException("投喂的米粒数不是一个整数");
        }
        if (riceGrainsNumber < 1 || riceGrainsNumber > 10) {
            throw new BusinessException("米粒投喂数不符合范围");
        }
        Long publisherId = articleService.queryPublisherIdByArticleId(articleId);
        if (Objects.isNull(publisherId)) {
            throw new DataNotExistException("投喂的文章不存在");
        }
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        if (publisherId.equals(userId)) {
            throw new DataNotExistException("不能给自己投喂哦");
        }
        //不使用security中的用户数据
        User user = userService.queryUserRiceGrainNumber(userId);
        Double userRiceGrainNumber = user.getRiceGrainNumber();
        //查询用户米粒余额
        if (userRiceGrainNumber < riceGrainsNumber) {
            throw new BusinessException("你的米粒数不够" + riceGrainsNumber.intValue() + "个了");
        }
        //查询用户历史投喂加上本次是否超出10个
        Integer feedCount = this.queryFeedArticleRiceGrainsNumber(articleId, userId);
        if (feedCount + riceGrainsNumber > 10) {
            throw new BusinessException("你已经投喂过" + feedCount + "个米粒啦!");
        }
        user.setRiceGrainNumber(userRiceGrainNumber - riceGrainsNumber);
        //更新用户的米粒数
        if (!userService.updateUserRiceGrainNumberById(user, userRiceGrainNumber)) {
            throw new UpdateDataBaseException("更新用户米粒数失败");
        }
        userRiceGrainsLogService.saveReduceRiceGrainsLog(userId, riceGrainsNumber, "投喂文章");

        //作者能获得的米粒数
        double publisherGetRiceGrainsNumber = riceGrainsNumber / 2;
        User publisher = userService.queryUserRiceGrainNumber(publisherId);
        Double publisherRiceGrainNumber = publisher.getRiceGrainNumber();
        publisher.setRiceGrainNumber(publisherRiceGrainNumber + publisherGetRiceGrainsNumber);
        //更新作者的米粒数
        if (!userService.updateUserRiceGrainNumberById(publisher, publisherRiceGrainNumber)) {
            throw new UpdateDataBaseException("更新作者米粒数失败");
        }
        userRiceGrainsLogService.saveIncreaseRiceGrainsLog(userId, publisherGetRiceGrainsNumber, "别人投喂了你的文章");
        //添加一个投喂记录
        UserArticleRiceGrains riceGrains = new UserArticleRiceGrains();
        riceGrains.setArticleId(articleId);
        //这里可以直接获取整数值,前面已经判断过了不存在小数
        riceGrains.setFeedRiceGrainsNumber(riceGrainsNumber.intValue());
        riceGrains.setUserId(userId);
        if (!this.save(riceGrains)) {
            throw new InsertDataBaseException("插入投喂记录失败");
        }
    }

    @Override
    public Integer queryFeedArticleRiceGrainsNumber(Long articleId, Long userId) {
        return userArticleRiceGrainsMapper.queryFeedArticleRiceGrainsNumber(articleId, userId);
    }
}
